package com.canaan.business.controller.pc.basic;

import java.util.List;

import com.canaan.business.domain.basic.WmsCodeRuleProduct;
import com.canaan.business.service.basic.IWmsCodeRuleProductService;
import com.canaan.common.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.enums.BusinessType;
import com.canaan.common.utils.poi.ExcelUtil;
import com.canaan.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物料条码规则Controller
 *
 * @author ruoyi
 * @date 2023-01-29
 */
@Api(tags = "物料条码规则")
@RestController
@RequestMapping("/system/product")
public class WmsCodeRuleProductController extends BaseController {
    @Autowired
    private IWmsCodeRuleProductService wmsCodeRuleProductService;

    /**
     * 查询物料条码规则列表
     */
    @ApiOperation(value = "查询物料条码规则列表")
    @GetMapping("/list")
    public TableDataInfo list(WmsCodeRuleProduct wmsCodeRuleProduct) {
        startPage();
        List<WmsCodeRuleProduct> list = wmsCodeRuleProductService.selectWmsCodeRuleProductList(wmsCodeRuleProduct);
        return getDataTable(list);
    }

    /**
     * 导出物料条码规则列表
     */
    @ApiOperation(value = "导出物料条码规则列表")
    @Log(title = "物料条码规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsCodeRuleProduct wmsCodeRuleProduct) {
        List<WmsCodeRuleProduct> list = wmsCodeRuleProductService.selectWmsCodeRuleProductList(wmsCodeRuleProduct);
        ExcelUtil<WmsCodeRuleProduct> util = new ExcelUtil<WmsCodeRuleProduct>(WmsCodeRuleProduct.class);
        util.exportExcel(response, list, "物料条码规则数据");
    }

    /**
     * 获取物料条码规则详细信息
     */
    @ApiOperation(value = "获取物料条码规则详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(wmsCodeRuleProductService.selectWmsCodeRuleProductById(id));
    }

    /**
     * 新增物料条码规则
     */
    @ApiOperation(value = "新增物料条码规则")
    @Log(title = "物料条码规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsCodeRuleProduct wmsCodeRuleProduct) {
        return toAjax(wmsCodeRuleProductService.insertWmsCodeRuleProduct(wmsCodeRuleProduct));
    }

    /**
     * 修改物料条码规则
     */
    @ApiOperation(value = "修改物料条码规则")
    @Log(title = "物料条码规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsCodeRuleProduct wmsCodeRuleProduct) {
        return toAjax(wmsCodeRuleProductService.updateWmsCodeRuleProduct(wmsCodeRuleProduct));
    }

    /**
     * 删除物料条码规则
     */
    @ApiOperation(value = "删除物料条码规则")
    @Log(title = "物料条码规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wmsCodeRuleProductService.deleteWmsCodeRuleProductByIds(ids));
    }


    /**
     * 物料条码规则导入模板
     */
    @ApiOperation(value = "物料条码规则导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<WmsCodeRuleProduct> util = new ExcelUtil<>(WmsCodeRuleProduct.class);
        util.importTemplateExcel(response, "物料条码规则");
    }


    /**
     * 导入物料条码规则
     */
    @PostMapping("/importExcel")
    @ApiOperation(value = "导入物料条码规则")
    @Log(title = "物料条码规则", businessType = BusinessType.IMPORT)
    public AjaxResult importExcel(MultipartFile file) {
        List<WmsCodeRuleProduct> list;
        try {
            //读取excel文件数据
            ExcelUtil<WmsCodeRuleProduct> util = new ExcelUtil<>(WmsCodeRuleProduct.class);
            list = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("导入文件解析失败");
        }
        //初步校验数据
        if (CollectionUtils.isEmpty(list)) {
            throw new ServiceException("文件缺少数据");
        }

        //深入校验数据和保存数据
        List<String> errorList = wmsCodeRuleProductService.importExcel(list);
        if (!errorList.isEmpty()) {
            StringBuilder failureMsg = new StringBuilder();
            failureMsg.append("很抱歉，导入失败！共 ").append(errorList.size()).append(" 条数据格式不正确，错误如下：");
            for (int i = 0; i < errorList.size(); i++) {
                failureMsg.append("<br/>").append(i + 1).append("、").append(errorList.get(i));
            }
            return error(failureMsg.toString());
        }
        return success("导入成功");
    }


    /**
     * 获取物料条码规则详细信息
     */
    @ApiOperation(value = "getBarcode")
    @GetMapping(value = "/getBarcode/{productNo}")
    public AjaxResult getBarcode(@PathVariable("productNo") String productNo) {
        return success(wmsCodeRuleProductService.getBarcode(productNo));
    }
}
